//
//  SJRootChoose.m
//  Recycling
//
//  Created by jzd on 2018/5/16.
//  Copyright © 2018年 宋佳. All rights reserved.
//

#import "SJRootChoose.h"
#import "LoginViewController.h"
#import "NewFeatureController.h"
#import "SJTabbarController.h"

@implementation SJRootChoose

// 加载哪个控制器
+ (UIViewController *)ChooseRootViewController
{
    UIViewController *rootVc = nil;
    
    NSDictionary *dict =  [NSBundle mainBundle].infoDictionary;
    // 获取最新的版本号
    NSString *curVersion = dict[@"CFBundleShortVersionString"];
    
    // 获取上一次的版本号
    NSString *lastVersion = [[NSUserDefaults standardUserDefaults] objectForKey:VersionKey];
    
    // 之前的最新的版本号 lastVersion
    if ([curVersion isEqualToString:lastVersion]) {
        // 版本号相等 由于token过期时间较短,所以每次启动app都要走一遍登录接口
        if ([UserInfoTool shareUserInfoTool].Info) {
            //之前登录过, 同步走一遍登录的接口, 失败回到登录页
            BOOL  result =  YES;
            if (result) {
                rootVc = [[SJTabbarController alloc]init];
            }else{
                rootVc = [[LoginViewController alloc]init];
            }
        }else{
            rootVc = [[LoginViewController alloc]init];
        }
    }else{ // 有最新的版本号
        // 保存到偏好设置
        rootVc = [[NewFeatureController alloc]init];
    }
    return rootVc;
}
/*
+(BOOL)synclogin{
    //url
    NSString *urlString = [NSString stringWithFormat:@"%@%@",SJSeverce,login];
    NSURL *url = [NSURL URLWithString:urlString];
    
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    
    NSString *timetamp = [NSString currentTimeStr];
    NSString *pwdStr = [NSString stringWithFormat:@"%@%@%@",appIdStr,appSecretStr,timetamp];
    NSString *signtime = [SJMD5Tool MD5ForLower32Bate:pwdStr];
    
    [request setValue:timetamp forHTTPHeaderField:@"timestamp"];
    [request setValue:appIdStr forHTTPHeaderField:@"appid"];
    [request setValue:signtime forHTTPHeaderField:@"signature"];
   
    [request setHTTPMethod:@"POST"];
    
    NSString *bodyStr = [NSString stringWithFormat:@"loginname=%@&loginpwd=%@&client_type=2",[UserInfoTool shareUserInfoTool].Info.LoginName, [UserInfoTool shareUserInfoTool].Info.MD5Pwd];
    SJLog(@"%@",[UserInfoTool shareUserInfoTool].Info);
    //将nstring转换成nsdata
    NSData *body = [bodyStr dataUsingEncoding:NSUTF8StringEncoding];
    //MyLog(@"body data %@", body);
    [request setHTTPBody:body];
    NSURLResponse *response = nil;
    NSError *error = nil;
    //第二，三个参数是指针的指针，所有要用取址符，这个方法是同步方法。同步操作没有完成，后面的代码不会执行。
    NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
        NSString *str = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
        SJLog(@"返回结果是：%@", str);
    if (error == nil) {  //接受到数据，表示工作正常
        
        NSError *error = nil;
        NSDictionary *responseObject = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error];
        if (error == nil) {
            int code = [[responseObject objectForKey:@"Code"] intValue];
            if (code == 1) {
                NSDictionary *dataDic = [responseObject objectForKey:@"Data"];
                UserInfo *user = [[UserInfo alloc]setValueDicts:dataDic];
                user.MD5Pwd = [UserInfoTool shareUserInfoTool].Info.MD5Pwd
                ;
                [NSKeyedArchiver archiveRootObject:user toFile:[UserInfoTool shareUserInfoTool].savePath];
                SJLog(@"%@",[UserInfoTool shareUserInfoTool].savePath);
                [UserInfoTool shareUserInfoTool].Info = user;
                return YES;
                
            }else{
                return NO;
            }
        }else{
            return NO;
        }
    }else{
        return NO;
    }
}
 */
@end
